package com.hospital.doctor.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.hospital.doctor.pojo.CheckBody;
import com.hospital.doctor.pojo.Prescription;
import com.hospital.doctor.service.CheckService;
import com.hospital.doctor.service.PatientService;
import com.hospital.doctor.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/check")
@CrossOrigin
public class CheckController {

    @Autowired
    private PatientService patientService;

    @Autowired
    private CheckService checkService;

    @RequestMapping("/insertCheck")
    public Map<String,Object> insertCheckWithPid(@RequestBody CheckBody checkBody, HttpServletRequest request){

        checkBody.setdId(Integer.valueOf(JWTUtils.verify(request.getHeader("token")).
                getClaim("id").asString()));
        System.out.println(checkBody.getcContent());
        Map<String,Object> map = new HashMap<>();
        map.put("msg",checkService.insertCheck(checkBody));
        return map;
    }
    @RequestMapping("/selectAllBypId/{pId}")
    public Map<String,Object> selectAllBypId(@PathVariable("pId") int pId){
        Map<String,Object> map = new HashMap<>();
        map.put("msg",checkService.selectAllBypId(pId));
        return map;
    }

    @RequestMapping("/selectAllBycId/{cId}")
    public Map<String,Object> selectAllBycId(@PathVariable("cId") int cId){
        Map<String,Object> map = new HashMap<>();
        map.put("msg",checkService.selectBycId(cId));
        return map;
    }



    //导出excel
    @RequestMapping("/download/{pId}/{pName}")
    public void upload(HttpServletResponse response, @PathVariable("pId") int pId, @PathVariable("pName") String pName) throws IOException {
        System.out.println(pId);
        System.out.println(pName);
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode(pName+"的检查", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), CheckBody.class).sheet("检查")
                    .doWrite(checkService.selectAllBypId(pId));
            System.out.println("+++++++++++");
        } catch (Exception e) {
            System.out.println("============");
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap<String, String>();
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));
        }
    }

}
